<template>
    <!-- :style="{paddingBottom:!transferIsSave?'182rpx':0}" -->
	<view class="content">
        <!-- navbar -->
        <u-navbar :fixed="true" :shadow="true" :safeAreaInsetTop="true" :placeholder="true"
            @leftClick="back" title="新增班组">
        </u-navbar>

        <!-- content -->
        <view class="uni-area">
            <!-- 班组信息 -->
            <uni-item-base-plus class="u-m-t-50" :baseData="team" @confirmSelect="confirmSelect" @inputChange="inputChange" />

            <!-- 原因 -->
            <!-- <view class="u-m-t-50 u-font-32 font-bold">新增原因</view>
            <u--textarea class="u-m-t-30 textarea" v-model="submitData.bz" :autoHeight="false" :height="66"
                placeholder="接口未提供新增原因的字段" placeholderClass="uni-placeholder">
            </u--textarea> -->

            <!-- 附件 -->
            <uni-accessory class="u-m-t-50" :title="{title:'附件'}" :accessorys="accessoryList"
                :upload="true" :uploadParameter="uploadParameter" @uploadSucceed="uploadSucceed">
            </uni-accessory>
        </view>

        <!-- 底部操作 -->
        <view class="uni-bottom u-flex u-row-between">
            <u-button class="btn3" text="取消" @tap="cancel"></u-button>
            <u-button class="btn4" type="primary" text="保存" @tap="submit"> </u-button>
        </view>
	</view>
</template>

<script>
export default {
    data() {
        return {
            parameter:{ // 参数
                deptId: uni.getStorageSync("userInfo").deptId, 
                gcbh: uni.getStorageSync("userInfo").ssgcbh
            },
            team:{ // 新增班组
                line: false,
                title: '班组信息',
                rightText: '',
                list:[
                    { show:true, field:'qymc', title:'施工企业', value: uni.getStorageSync("userInfo").ssqymc },
                    { show:true, field:'gcmc', title:'项目名称', value: uni.getStorageSync("userInfo").ssgcmc },
                    { show:true, field:'deptName', title:'所属企业', value:'', type:'select', pla:'请选择所属企业', data:[] },
                    { show:true, field:'bzmc', title:'班组名称', value:'', type:'input', pla:'请输入班组名称' },
                ],
            },
            submitData:{ // 提交数据
                id: null,  // 主键【必传】
                sjly: "app", // 数据来源【必填】
                gcbh: uni.getStorageSync("userInfo").ssgcbh, // 工程编号【必传】
                gcmc: uni.getStorageSync("userInfo").ssgcmc, // 工程名称【必传】
                deptId: "", // 所属部门【必传】
                bzmc: "", // 班组名称【必传】
                // bz: '', // 新增原因
            },
            accessoryList:[], // 附件
            uploadParameter: { // 上传附件的参数
                ywlx: 'salary_smzgl_xmbz',
            },
            id: null, // 修改新增班组的业务Id
        }
    },


    onLoad(option) {
        this.getEntList() // 获取所属企业
        if( !option.id ) { // 获取业务编号
            this.getYwbh()
        }else{ // 修改新增班组
            this.id = option.id
            this.getGroupDetail()
        }
    },


    methods: {
        // 返回
        back(){
            uni.navigateBack()
        },


        // 所属企业
        getEntList(){
            this.$request(this,this.$apis.belongEntList,this.parameter).then(res=>{
                // console.log('所属企业',res)
                res.map((v,i)=>{
                    this.team.list[2].data.push({ label: v.deptName, value: v.deptId })
                })
            }).then()
        },
        // 选择所属企业
        confirmSelect(e){
            // console.log('下拉选择',e)
            this.submitData.deptId = e.value
            this.team.list[2].value = e.label
        },


        // 获取业务编号
        getYwbh(){
            this.$request(this,this.$apis.GetYwbh,{ ywlx:this.uploadParameter.ywlx }).then(res=>{
                // console.log('业务编号',res)
                this.submitData.id = res
                this.uploadParameter.bid = res
                this.getAccessoryList()
            }).then()
        },
        // 班组详情
        getGroupDetail(){
            this.$request('this',this.$apis.addGroupDetail,{id:this.id}).then(res=>{
                console.log('班组详情',res)
                // 回填视图
                this.team.list.map((v,i)=>{
                    v.value = res[v.field]
                })
                // 回填数据
                for(let key in this.submitData){
                    this.submitData[key] = res[key]
                }
                // 附件
                this.uploadParameter.bid = res.id
                this.getAccessoryList()
            })
        },


        // 输入框改变事件
        inputChange(e){
            // console.log('输入框改变',e)
            this.submitData.bzmc = e.value
            this.team.list[3].value = e.value
        },


        // 申请提交&&修改
        submit(){
            if( !this.submitData.deptId ) {
                uni.$u.toast('请选择所属企业')
                return false
            }
            if( !this.submitData.bzmc ) {
                uni.$u.toast('请填写班组名称')
                return false
            }
            // if( !this.submitData.bz ) {
            //     uni.$u.toast('请填写新增原因')
            //     return false
            // }
            // console.log('提交参数',this.submitData)
            let api
            if( !this.id ){
                api = this.$apis.addGroupSubmit
            }else{
                api = this.$apis.addGroupEdit
            }
            uni.showLoading({ title:'保存中' })
            this.$request(this,api,this.submitData).then(res=>{
                uni.navigateBack()
            }).then()
        },


        // 附件列表
        getAccessoryList(){
            const parameter = {
                ywlx: this.uploadParameter.ywlx, // 业务类型
                ywbh: this.uploadParameter.bid, // 业务编号
            }
            uni.showLoading({ title:'附件列表获取中' })
            this.$request(this,this.$apis.MemberAtta,parameter).then(res=>{
                this.accessoryList = res
                if( res ) this.uploadParameter.attachType = res[0].attachType
            }).then()
        },
        // 上传附件成功
        uploadSucceed(){
            this.getAccessoryList()
        },


        // 取消
        cancel(){
            this.$tools.showModal('提示','未保存是否继续退出？',res=>{
                this.back()
            })
        },
    }
}
</script>

<style scoped lang="scss">
    .content{
        // 文本域
        .textarea{
            border: none;
            padding: 40rpx 30rpx;
            background: #f4f5f7;
            border-radius: 16rpx;
            /deep/.uni-textarea-textarea{
                font-size: 28rpx !important;
            }
        }
    }
</style>
